﻿using CustomCharacter;
using CustomCharacter.Ability;
using CustomPlayerEffects;
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using InventorySystem;
using MEC;
using PlayerRoles;
using PlayerStatsSystem;
using RgCore.Events;
using RgCore.Misc;
using RgCore.PlayerHud;
using System.Collections.Generic;
using CustomCharacter.Interfaces;
using RgCore.CustomSide;
using UnityEngine;

namespace Scp1793
{
    [RoleInfo("SCP-1793", "Human", RoleTypeId.FacilityGuard, RoleTypeId.Tutorial, MaxHealth = 70)]
    public class Scp1793 : CustomCharacter.CustomCharacter,
        IScpNumber, ICustomSpawnpoint, IRoundStartSpawn, ITrialCard
    {
        public Scp1793(int id) : base(id)
        {
        }

        public int Cost => 400;
        public Vector3 SpawnPosition => Exiled.API.Extensions.RoleExtensions.GetRandomSpawnLocation(PlayerRoles.RoleTypeId.FacilityGuard).Position;

        private static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (e == null || trigger != AbilityTrigger.Event)
                return new AbilityOutput(false);
            if (e is ChangingMicroHIDStateEventArgs ev1 && ev1.Player == player && ev1.NewState == InventorySystem.Items.MicroHID.HidState.PoweringUp)
            {
                ev1.IsAllowed = false;
                return new AbilityOutput(true);
            }
            if (e is InteractingDoorEventArgs ev2 && ev2.Player == player && ev2.Door.IsCheckpoint)
            {
                ev2.IsAllowed = true;
                return new AbilityOutput(true);
            }
            if (e is HurtingEventArgs ev3)
            {
                if ((ev3.DamageHandler.Base is JailbirdDamageHandler && ev3.Attacker == player) || (ev3.Attacker?.Role == RoleTypeId.Scp173 && ev3.Player == player))
                {
                    ev3.IsAllowed = false;
                    ev3.Amount = 0f;
                }
                return new AbilityOutput(true);
            }
            if (e is HandcuffingEventArgs ev4 && (ev4.Target == player || ev4.Player == player))
            {
                ev4.IsAllowed = false;
                return new AbilityOutput(true);
            }
            if (e is ShootingEventArgs ev5 && ev5.Player == player)
            {
                ev5.IsAllowed = false;
                if (!player.HasMsg("cannot_shoot"))
                    player.SendRawMsg("cannot_shoot", $"由于技能限制，你的角色无法射击", 2, ScreenLocation.CenterBottom, true);
                return new AbilityOutput(true);
            }
            return new AbilityOutput(false);
        }

        private static AbilityOutput Ability1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            ItemType[] ava = new ItemType[]
            {
                ItemType.Adrenaline, ItemType.AntiSCP207, ItemType.GrenadeFlash, ItemType.GrenadeHE, ItemType.KeycardChaosInsurgency, ItemType.KeycardContainmentEngineer,
                ItemType.KeycardFacilityManager, ItemType.KeycardGuard, ItemType.KeycardJanitor, ItemType.KeycardMTFPrivate, ItemType.KeycardMTFOperative, ItemType.KeycardMTFCaptain,
                ItemType.KeycardResearchCoordinator, ItemType.KeycardScientist, ItemType.KeycardZoneManager, ItemType.KeycardO5, ItemType.Medkit,
                ItemType.MicroHID, ItemType.Painkillers, ItemType.Radio, ItemType.SCP018, ItemType.SCP1576, ItemType.SCP1853, ItemType.SCP207,
                ItemType.SCP268, ItemType.SCP500, ItemType.SCP2176, ItemType.SCP244a, ItemType.SCP244b, ItemType.SCP330};
            player.Inventory.ServerAddItem(ava[UnityEngine.Random.Range(0, ava.Length)]);
            List<Player> scpAffected = ListPool<Player>.Pool.Get();
            List<Player> humanAffected = ListPool<Player>.Pool.Get();
            foreach (Player player1 in Player.List)
            {
                if (player1.IsAlive && Vector3.Distance(player1.Position, player.Position) <= 8f && player1.Role != RoleTypeId.Scp106 && player1.Role != RoleTypeId.Scp079 && player1 != player)
                {
                    if (!player.IsFriendly(player1))
                    {
                        if (player1.GetSide() == Side.Scp)
                        {
                            scpAffected.Add(player1);
                        }
                        else
                        {
                            humanAffected.Add(player1);
                        }
                    }
                }
            }

            if (humanAffected.Count > 0 && scpAffected.Count == 0)
            {
                return new AbilityOutput(true, "毒素释放失败，附近没有威胁");
            }
            else
            {
                foreach (Player p in scpAffected)
                {
                    p.EnableEffect<Poisoned>(60, true);
                    p.SendRawMsg("", "<color=red>你受到了SCP-1793技能的影响！</color>", 5, ScreenLocation.CenterBottom, true);
                }

                foreach (Player p in humanAffected)
                {
                    p.EnableEffect<Poisoned>(20, true);
                    p.SendRawMsg("", "<color=yellow>你受到了SCP-1793技能的影响！</color>", 5, ScreenLocation.CenterBottom, true);
                }
            }
            int count = scpAffected.Count;
            ListPool<Player>.Pool.Return(scpAffected);
            ListPool<Player>.Pool.Return(humanAffected);
            player.AddScore(count * 200, "影响威胁");
            return new AbilityOutput(true, $"本次技能影响到了<color=yellow> {count} </color>个威胁");
        }

        public override void Role_Activate()
        {
            base.Role_Activate();
            Player.Scale = Vector3.one * 0.3f;
        }

        public override void Role_Deactive()
        {
            base.Role_Deactive();
            Timing.CallDelayed(1.0f, delegate ()
            {
                Player.Scale = Vector3.one;
            });
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new Ability("Scp1793_1", "随机获得一个非武器物品，并对附近8m内的任何玩家造成一定时间的中毒效果", AbilityType.Offensive,
    90, Ability1, null, null, null, GetType()),
                new AttributeAbility("Scp1793_Attr_1", "无法使用武器；无需钥匙卡访问检查点；免疫SCP-173伤害", Attribute_1,
                false, new IExiledEvent[] {
                    Exiled.Events.Handlers.Player.ChangingMicroHIDState,
                    Exiled.Events.Handlers.Player.InteractingDoor,
                    Exiled.Events.Handlers.Player.Handcuffing,
                    Exiled.Events.Handlers.Player.Shooting,
                    EventProcessor.OnHurting
                }, null, GetType())
            };
        }

        public override Color32 RoleColor => new Color32(255, 0, 255, 255);

        public string ScpNumber => "1793";

        public byte MinPlayers => 10;
    }
}